package com.lzw.mall.order.controller;

import com.lzw.mall.common.entity.Member;
import com.lzw.mall.common.exception.BusinessException;
import com.lzw.mall.common.service.IMemberService;
import com.lzw.mall.common.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/")
public class RegisterController {

    @Autowired
    private IMemberService memberService;

    @Autowired
    private RedisTemplate<String,Object> redisTemplat;
    @GetMapping("/reg")
    @ResponseBody
    public void register(String phone, String code,String username,String password,String passwordRepeat){
        Object obj=redisTemplat.opsForValue().get("reg-code-"+phone);
        if(memberService.selectByPhone(phone)){
            throw new BusinessException("手机号已经被注册");
        }
        if (obj == null || !((String)obj).equals(code)) {
            throw new BusinessException("验证码输入不正确");
        }
        if (memberService.selectByUsername(username)&&memberService.selectByPhone(username)){
            throw new BusinessException("用户名已被使用");
        }
        if (!password.equals(passwordRepeat)) {
            throw new BusinessException("两次的密码输入不一致");
        }
        Member m=new Member();
        m.setUsername(username);
        m.setPhone(phone);
        m.setPassword(StringUtil.md5(password));
        memberService.doInsert(m);
    }
}
